package com.keyinong.caizhengting.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.keyinong.caizhengting.activity.DingdandetailActivity;
import com.keyinong.caizhengting.activity.ErweimaActivity;
import com.keyinong.caizhengting.activity.FanqiedingcanActivity;
import com.keyinong.caizhengting.entity.MyorderEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * @author：郑谦
 * @时间：2017-3-20 下午3:47:52
 * @类名：MyteamorderAdapter.java
 * @功能: 我的订单适配器
 */

public class MyteamorderAdapter extends BaseAdapter {
    Context context;
    List<MyorderEntity> myorderList;
    HttpTools httpTools = null;
    View iscatview = null;
    int mType;

    public MyteamorderAdapter(Context context, List<MyorderEntity> myorderList, int type) {
        super();
        this.context = context;
        this.myorderList = myorderList;
        this.mType = type;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myorderList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return myorderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHandle handle;
        if (view == null) {
            handle = new ViewHandle();
            view = LayoutInflater.from(context).inflate(R.layout.item_myorder, null);
            handle.tv_xiadanshijian = (TextView) view.findViewById(R.id.tv_xiadanshijian);
            handle.tv_state = (TextView) view.findViewById(R.id.tv_state);
            handle.tv_caipin = (TextView) view.findViewById(R.id.tv_caiming);
            handle.tv_price = (TextView) view.findViewById(R.id.tv_price);
            handle.btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
            handle.btn_xiangqiang = (Button) view.findViewById(R.id.btn_dingdanxiangqing);
            handle.btn_quzhifu = (Button) view.findViewById(R.id.btn_quzhifu);
            view.setTag(handle);
        } else {
            handle = (ViewHandle) view.getTag();
        }
        handle.tv_caipin.setText(myorderList.get(position).getName() + "等" + myorderList.get(position).getCount() + "个菜品");
//		handle.tv_price.setText(""+(int)(Double.parseDouble(myorderList.get(position).getZongjia())*10));
        handle.tv_price.setText(myorderList.get(position).getZongjia());
        handle.tv_xiadanshijian.setText(myorderList.get(position).getOrder_time());
        if (myorderList.get(position).getState().equals("-1")) {
            handle.tv_state.setText("已取消");
            handle.btn_quzhifu.setVisibility(View.GONE);
            handle.btn_cancle.setVisibility(View.GONE);
            handle.btn_xiangqiang.setVisibility(View.VISIBLE);
            handle.btn_xiangqiang.setText("订单详情");
        } else if (myorderList.get(position).getState().equals("0")) {
            handle.tv_state.setText("待支付");
            handle.btn_quzhifu.setVisibility(View.VISIBLE);
            handle.btn_cancle.setVisibility(View.VISIBLE);
            handle.btn_xiangqiang.setVisibility(View.GONE);
            //				handle.btn_zailaiyidan.setVisibility(View.INVISIBLE);
        } else if (myorderList.get(position).getState().equals("1")) {
            if (mType== 0) {
                handle.tv_state.setText("待取餐");
                handle.btn_xiangqiang.setText("取餐二维码");
            } else {
                handle.tv_state.setText("待取货");
                handle.btn_xiangqiang.setText("取货二维码");
            }

            handle.btn_quzhifu.setVisibility(View.GONE);
            handle.btn_cancle.setVisibility(View.VISIBLE);
            handle.btn_xiangqiang.setVisibility(View.VISIBLE);


        } else if (myorderList.get(position).getState().equals("2")) {
            handle.tv_state.setText("已完成");
            handle.btn_quzhifu.setVisibility(View.GONE);
            handle.btn_cancle.setVisibility(View.GONE);
            handle.btn_xiangqiang.setVisibility(View.VISIBLE);
            handle.btn_xiangqiang.setText("订单详情");
        }


        /**去支付*/
        handle.btn_quzhifu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                paydialog(position);
            }
        });

        /**订单详情*/
        handle.btn_xiangqiang.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();

                if (myorderList.get(position).getState().equals("-1") || myorderList.get(position).getState().equals("2")) {
                    intent.putExtra("orderid", myorderList.get(position).getId());
                    intent.putExtra("type", mType);
                    intent.setClass(context, DingdandetailActivity.class);
                    context.startActivity(intent);
                } else if (myorderList.get(position).getState().equals("1")) {
                    intent.putExtra("id", myorderList.get(position).getId());
                    intent.setClass(context, ErweimaActivity.class);
                    context.startActivity(intent);
                }

            }
        });
        /**取消订单*/
        handle.btn_cancle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                cancledialog(position, handle.btn_cancle);
//				handle.btn_cancle.setEnabled(false);
            }
        });
        return view;
    }

    private void Balancepay(final int index) {
        SharedPreferences sp = context.getSharedPreferences("spReg", Context.MODE_PRIVATE);
        String token = sp.getString("data", "");

        //		Log.i("zq", "balancepayorderid---"+orderid);
        if (httpTools == null) {
            httpTools = new HttpTools(context);
        }

//		httpTools.submitpay(token,myorderList.get(index).getId(),new JsonHttpResponseHandler() {
//
//			@Override
//			public void onSuccess(int statusCode, Header[] headers,
//					JSONObject response) {
//				super.onSuccess(statusCode, headers, response);
//				try {
//					boolean code = response.getBoolean("code");
//					String msg = response.getString("msg");
//					Log.i("zq", "balancepay---"+response.toString());
//					if (code == true) {
//						//						String data=response.getString("data");
//						Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
//						Intent intent = new Intent();
//						intent.putExtra("code", "2");
//						intent.setClass(context, FanqiedingcanActivity.class);
//						context.startActivity(intent);
//						//						finish();
//					} else {
//						//						flag=false;
//						Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
//						//						finish();
//					}
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//
//		});
    }

    /**
     * 支付弹窗
     */
    private void paydialog(final int index2) {

        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        iscatview = LayoutInflater.from(context).inflate(
                R.layout.dialog_pay, null);
        TextView tital = (TextView) iscatview.findViewById(R.id.tital);
        TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
        TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
        tital.setText("是否立即支付?");
        dialog.setContentView(iscatview);
        dialog.show();
        tv_no.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });
        tv_yes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Balancepay(index2);
                dialog.dismiss();
            }
        });
    }

    /**
     * 取消订单弹窗
     */
    private void cancledialog(final int index2, final Button btn) {

        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        iscatview = LayoutInflater.from(context).inflate(
                R.layout.dialog_pay, null);
        TextView tital = (TextView) iscatview.findViewById(R.id.tital);
        TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no);
        TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes);
        tital.setText("确定取消该订单？");
        dialog.setContentView(iscatview);
        dialog.show();
        tv_no.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                btn.setEnabled(true);
            }
        });
        tv_yes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // Log.e("lh", "iscat-----"+iscat);
                //				CallPhone.phone(LineDetailActivity.this, phone);
                if (NetWorkUtil.isNetwork(context) == false) {
                    Toast.makeText(context, "当前网络不可用", Toast.LENGTH_SHORT).show();
                    return;
                }
                SharedPreferences sp = context.getSharedPreferences("spReg",
                        Context.MODE_PRIVATE);
                String token = sp.getString("data", "");

                if (httpTools == null) {
                    httpTools = new HttpTools(context);
                }
                httpTools.cancelorder(token, myorderList.get(index2).getId(),
                        new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers,
                                                  JSONObject response) {
                                super.onSuccess(statusCode, headers, response);
                                try {
                                    boolean code = response.getBoolean("code");
                                    String msg = response.getString("msg");
                                    if (code == true) {
                                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent();
                                        intent.putExtra("code", "2");
                                        intent.setClass(context, FanqiedingcanActivity.class);
                                        context.startActivity(intent);
                                        btn.setEnabled(true);
                                    } else {
                                        btn.setEnabled(true);
                                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                                    }
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                        });
                dialog.dismiss();
            }
        });
    }

    class ViewHandle {
        /**
         * 下单时间
         */
        TextView tv_xiadanshijian;
        /**
         * 订餐状态
         */
        TextView tv_state;
        /**
         * 菜品名
         */
        TextView tv_caipin;
        /**
         * 价格
         */
        TextView tv_price;
        /**
         * 取消订单
         */
        Button btn_cancle;
        /**
         * 订单详情
         */
        Button btn_xiangqiang;
        /**
         * 去支付
         */
        Button btn_quzhifu;
    }

    /**删除订单弹窗*/
    //	private void delatedialog(final int index3) {
    //
    //		final Dialog dialog = new Dialog(context);
    //		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //
    //		iscatview = LayoutInflater.from(context).inflate(
    //				R.layout.dialog_pay, null);
    //		TextView tital = (TextView) iscatview.findViewById(R.id.tital);
    //		TextView tv_quxiao = (TextView) iscatview.findViewById(R.id.tv_balance);
    //		TextView tv_queding = (TextView) iscatview.findViewById(R.id.tv_zhifubao);
    //		tital.setText("确定删除该订单记录？");
    //		tv_quxiao.setText("取消");
    //		tv_queding.setText("确定");
    //		dialog.setContentView(iscatview);
    //		dialog.show();
    //		tv_quxiao.setOnClickListener(new OnClickListener() {
    //
    //			@Override
    //			public void onClick(View v) {
    //				dialog.dismiss();
    //			}
    //		});
    //		tv_queding.setOnClickListener(new OnClickListener() {
    //
    //			@Override
    //			public void onClick(View v) {
    //				deletedingdan2(index3);
    //				dialog.dismiss();
    //			}
    //		});
    //	}

}
